<?php

class FileOpsComponent extends Object
{

	/**
	 *	function startup()
	 **/
    function startup(&$controller)
    {
    	$this->controller = &$controller;
    }
	
	function movefile($destdir, $tempfile, $savefile)
	{
		$movefile = move_uploaded_file($tempfile, $destdir . $savefile);
		return $movefile;
	}
	
	# OPTIMIZE: This could probably be done more ellegantly
	function explodeFilename($filename)
	{
		$output = array();
		$output['filename'] =  explode(".", $filename); // split into array around periods
		$output['ext'] = array_pop($output['filename']); // remove last element of array; set aside as extension
		$output['filename'] = implode(".", $output['filename']); // rejoin file name minus extension
		$output['filename'] = $this->fnFix($output['filename']); //Fix filename
		return $output;
	}
	
	function fnFix($filename) {
		$filtered_filename = "";
		
		$patterns = array(
							"/\s/", # Whitespace
							"/\&/", # Ampersand
							"/\+/"  # Plus
							);
		$replacements = array(
								"_",   # Whitespace
								"and", # Ampersand
								"plus" # Plus
								);
		
		$filename = preg_replace($patterns,$replacements,$filename);
		for ($i=0;$i<strlen($filename);$i++) {
			$current_char = substr($filename,$i,1);
			if (ctype_alnum($current_char) == TRUE || $current_char == "_" || $current_char == ".") {
				$filtered_filename .= $current_char;
			}
		}     
			
		return $filtered_filename;
	}
	
}
?>